package cycling.taxi.com;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import cycling.taxi.com.bean.LocationInfo;
import cycling.taxi.com.dialog.OnPositionListener;
import cycling.taxi.com.eventbus.SendMassage;
import cycling.taxi.com.request.Config;
import cycling.taxi.com.request.RM;
import cycling.taxi.com.request.RequestBean;
import cycling.taxi.com.request.RequestCallback;
import cycling.taxi.com.request.RequestParams;
import cycling.taxi.com.request.RequestUtil;
import cycling.taxi.com.utils.LogUtil;
import cycling.taxi.com.utils.MD5Util;
import cycling.taxi.com.utils.SPUtil;
import cycling.taxi.com.utils.ToastUtil;
import cycling.taxi.com.utils.Urls;

/**
 * 定时获取数据的服务
 * Created by lqh123 on 2017/5/10.
 */
public class MyService extends Service implements RequestCallback{
    private MyThread myThread;
    private int i=0;
    private  String position=null;
    boolean isClose=true;

    /**
     * 更新位置的回调接口
     */
    private OnPositionListener OnPositionListener;
    /**
     * 注册回调接口的方法，供外部调用
     * @param
     */
    public void setOnPositionListener(OnPositionListener OnPositionListener) {
        this.OnPositionListener = OnPositionListener;
    }
    /**
     * 增加get()方法，供Activity调用
     * @return 位置信息
     */
    public String getPosition() {
        return position;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MsgBinder();
    }

    @Override
    public void onCreate() {
        isClose=true;
        this.myThread = new MyThread();
        this.myThread.start();
        super.onCreate();
        EventBus.getDefault().register(this);
    }

        @Override
        public void requestSuccess(RequestBean bean, Config config) {
            LogUtil.e("服务返回的数据="+bean.beanJson);
            if (OnPositionListener!=null){
                    OnPositionListener.onPosition(bean.getStatusCode(),bean.beanJson);
            }
        }

        @Override
        public void requestFail(RequestBean bean, Config config) {
            LogUtil.e("请求失败");
            if (OnPositionListener!=null){
                OnPositionListener.onPosition(bean.getStatusCode(),bean.beanJson);
            }
        }

        @Override
        public void requestTokenInvalid(String requestTag, Config config) {
            LogUtil.e("Token失效");
        }



    private class MyThread extends Thread {
        @Override
        public void run() {
            while (isClose) {
                LogUtil.d("发送请求");
                try {
                    i++;
                    // 每个2秒向服务器发送一次请求
                    Thread.sleep(5000);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //下面写请求服务器的代码
                RequestParams params = new RequestParams.Builder()
                        .putParam("mobile", SPUtil.getInstance().getData("phone"))
                        .putParam("password", MD5Util.MD5Encode(SPUtil.getInstance().getData("password")))
                        .putParam("beanJson",SPUtil.getInstance().getData("beanJson"))
//                        .putParam("imei",SPUtil.getInstance().getData("facilityCode"))
                        .build();
                RequestQueue requestQueue = Volley.newRequestQueue(MyService.this);

                String url = Urls.getLocation();//回去api路径

                RequestUtil.requestData(requestQueue,
                        IConstants.LOCATION,
                        url,
                        RM.GET,
                        LocationInfo.class,
                        params,
                        MyService.this);
            }
        }
    }
    public class MsgBinder extends Binder {
        /**
         * 获取当前Service的实例
         * @return
         */
        public MyService getService(){
            return MyService.this;
        }
    }

    @Subscribe
    public void onEvent(SendMassage mag){
        isClose=false;
        //ToastUtil.getInstance().toast("服务关闭");
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        LogUtil.e("服务关闭了"+isClose);
    }

}
